<?php

namespace app\admin\controller\content;

use app\common\controller\Backend;
use app\model\Banner as BannerModel;

/**
 * Material_Management
 *
 * @icon  fa fa-tv
 */
class Banner extends Backend
{
    /**
     * View
     */
    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams(null,null);

            $bannerModelHandle = new BannerModel();
            $list = $bannerModelHandle
                ->where($where)
                ->order($sort, 'asc')
                ->paginate($limit);

//            $data = $list->items();

            $result = array("total" => $list->total(), "rows" => $list->items(),"extend"=>[]);

            return json($result);
        }
        $this->view->assign('shangjia_tag',__('On Off'));
        return $this->view->fetch();

    }

    /**
     * Add
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $msg = $this->validate($params,'Banner');
            if($msg != 'true')
                $this->error($msg);

            $params['create_time'] = $params['update_time'] = date("Y-m-d H:i:s");
            unset($params['id']);
            db('banner')->insertGetId($params);
            $this->success(__('Add Success'));
        }

        $bannerModelHandle = new BannerModel();
        $positionData = $bannerModelHandle->getPosition();
        $this->view->assign('positionData',$positionData);
        $this->assignconfig('positionJson',json_encode($positionData));
        $this->view->assign('id',0);
        $this->view->assign('data',[]);
        $this->view->assign('type',1);
        $this->view->assign('tips','');
        return $this->view->fetch('addbanner');

    }

    /**
     * Edit
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            $msg = $this->validate($params,'Banner');
            if($msg != 'true')
                $this->error($msg);

            $params['update_time'] = date("Y-m-d H:i:s");
            unset($params['id']);
            db('banner')->where('id',$ids)->update($params);
            $this->success(__('Operation Success'));
        }

        $bannerModelHandle = new BannerModel();
        $data = db('banner')->where('id',$ids)->find();

        $tips = '';
        if($ids == 2) {
            $tips = __('sucai_rate', ['100:66']);
        }
        if($ids == 3) {
            $tips = __('sucai_rate', ['100:47']);
        }
        if($ids == 4) {
            $tips = __('sucai_rate', ['100:66']);
        }
        if($ids == 5) {
            $tips = __('sucai_rate', ['100:66']);
        }
        if($ids == 6) {
            $tips = __('sucai_rate', ['100:66']);
        }
        if($ids == 7) {
            $tips = __('sucai_rate', ['100:66']);
        }
        if($ids == 8) {
            $tips = __('sucai_rate', ['100:66']);
        }
        if($ids == 9) {
            $tips = __('sucai_rate', ['100:66']);
        }
        if($ids == 10) {
            $tips = __('sucai_rate', ['100:66']);
        }
        if($ids == 11) {
            $tips = __('sucai_rate', ['100:66']);
        }
        if($ids == 12) {
            $tips = __('sucai_rate', ['100:100']);
        }
        $positionData = $bannerModelHandle->getPosition();
        $this->view->assign('positionData',$positionData);
        $this->view->assign('id',$ids);
        $this->view->assign('data',$data);
        $this->view->assign('type',$data['type']);
        $this->view->assign('tips',$tips);
        return $this->view->fetch('addbanner');
    }

    /**
     * Del
     */
    public function del($ids = null)
    {
        if(empty($ids))
            $this->error(__('Error Parame'));

        //在排課系統没有的话就删除
        $ids = explode(',',$ids);
        foreach ($ids as $id){
//            db('banner')->where('id',$id)->delete();

        }
        $this->success(__('Operation Success'));
    }

    /**
     * Status modify
     */
    public function upbanner($ids = null)
    {
        if(empty($ids))
            $this->error(__('Error Parame'));

        $ids = explode(',',$ids);
        foreach ($ids as $id){
            $findOne = db('banner')->find($id);
            if(empty($findOne)){
                continue;
            }
            $up = [];
            $up['status'] = $findOne['status'] == 1 ? -1 : 1;
            db('banner')->where('id',$id)->update($up);
        }

        $this->success(__('Operation Success'));
    }
}
